package cn.turing.firecontrol.device.rest;

import cn.turing.firecontrol.auth.client.annotation.CheckClientToken;
import cn.turing.firecontrol.auth.client.annotation.CheckUserToken;
import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author : 赵贤
 * @create: 2023-03-24 11:27
 * @Description:
 */
@RestController
@RequestMapping("file")
@Slf4j
@CrossOrigin
public class FileController {

    @Autowired
    private FileService fileService;


    @RequestMapping("postFileUpload")
    public Object postFileUpload(@ModelAttribute Chunk chunk) throws InterruptedException {

        fileService.postFileUpload(chunk);

        if (chunk.getTotalChunks().equals(chunk.getChunkNumber())) { //检查分块是否全部上传
            TimeUnit.SECONDS.sleep(2);
            FileResult fileResult = fileService.mergeFile(chunk);
            return fileResult;
        }
        log.info("文件上传成功");
        return null;
    }

}
